package com.lx.person;

import com.lx.person.entity.*;

import java.util.Scanner;

/**
 * @author 柳新宽
 * @create 2022-03-21  10:32
 * @describe
 */
public class PersonManager {
    static Person ps [] = new Person[20];
    Scanner sc = new Scanner(System.in);
    static{
        Student stu = new Student();
        stu.setClasses("T6");
        stu.setCardId("123");
        stu.setName("李立");
        stu.setGender("男");
        stu.setHomeTown("河南南阳");

        DoorKeeper doorKeeper = new DoorKeeper();
        doorKeeper.setCardId("456");
        doorKeeper.setName("季木吉");
        doorKeeper.setGender("男");
        doorKeeper.setHomeTown("河南南阳");
        doorKeeper.setDept("燕窝");
        doorKeeper.setSalary(15000);

        DoorKeeper doorKeeper2 = new DoorKeeper();
        doorKeeper2.setCardId("789");
        doorKeeper2.setName("有博园");
        doorKeeper2.setGender("男");
        doorKeeper2.setHomeTown("河南商丘");
        doorKeeper2.setDept("猪圈");
        doorKeeper2.setSalary(25000);

        Athlete athlete = new Athlete();
        athlete.setCardId("101");
        athlete.setName("林白村");
        athlete.setGender("女");
        athlete.setHomeTown("北京海淀");
        athlete.setSport("花样游泳");
        athlete.setMedal("2024年巴黎奥运会花样游泳第98名");
        ps[0]=stu; //向上转型
        ps[1]=doorKeeper;//向上转型
        ps[2]=doorKeeper2;//向上转型
        ps[3]=athlete;
    }

    //展示所有的人口信息
    public void showAllPerson(){
        for (Person p : ps) { //person 里面存储都是 学生或者保安的对象
            if(p!=null){
                p.showMe();
            }
        }
    }

    public boolean addPerson(Person person){
        for (int i = 0; i < ps.length; i++) {
            if(ps[i]==null){
                ps[i]=person;
                return true; //结束方法
            }
        }
        return false;
    }
    //根据身份证号判断是否已存在此人口
    public boolean findPersonExistsByCardId(String cardId){
        for (Person p : ps) {
            if(p!=null&&p.getCardId().equals(cardId)){
                return true;
            }
        }
        return false;
    }
    //根据身份证号删除人口信息
    public boolean deletePersonByCardId(String cardId){
        for (int i = 0; i < ps.length; i++) {
            if(ps[i]!=null&&ps[i].getCardId().equals(cardId)){
                ps[i]=null;
                return true;
            }
        }
        return false;
    }

    //根据身份证号修改人口信息
    public boolean updatePersonByCardId(Person person){
        for (int i = 0; i < ps.length; i++) {
            if(ps[i]!=null&&ps[i].getCardId().equals(person.getCardId())){
                ps[i]=person;
                return true;
            }
        }
        return false;
    }
    //抽离公共代码
    public Person inputUpdateOrAddMessage(String cardId){
        System.out.println("请输入人口信息的姓名");
        String name = sc.next();
        System.out.println("请输入人口信息的性别");
        String gender = sc.next();
        System.out.println("请输入人口信息的籍贯");
        String homeTown = sc.next();
        System.out.println("请输入人口的职业(学生/运动员/保安/老师)");
        String key = sc.next();
        Person person = new Person();
        switch (key){
            case "学生" :
                Student stu = new Student();
                System.out.println("请收入班级的信息");
                stu.setClasses(sc.next());
                person=stu; //向上转型
                break;
            case "保安" :
                DoorKeeper doorKeeper = new DoorKeeper();
                System.out.println("请收入所属小区");
                doorKeeper.setDept(sc.next());
                System.out.println("请收入薪资");
                doorKeeper.setSalary(sc.nextDouble());
                person=doorKeeper;
                break;
            case "运动员" :
                Athlete athlete = new Athlete();
                System.out.println("请收入所属运动");
                athlete.setSport(sc.next());
                System.out.println("请收入获奖记录");
                athlete.setMedal(sc.next());
                person=athlete;
                break;
            case "老师" :
                Teacher teacher = new Teacher();
                System.out.println("请输入你的等级");
                teacher.setLevel(sc.nextInt());
                person= teacher;
                break;
            default:
                break;
        }//switch
        person.addFild(cardId,name,gender,homeTown);
        return person;
    }
}
